﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public delegate void ValueChangedHandler<T>(T oldValue, T newValue);

/// <summary>
/// 可绑定属性
/// </summary>
/// <typeparam name="T"></typeparam>
public class BindableProperty<T>
{
    public ValueChangedHandler<T> OnValueChanged;

    private T _value;
    public T Value
    {
        get
        {
            return _value;
        }
        set
        {
            if (!Equals(_value, value))
            {
                T old = _value;
                _value = value;
                ValueChanged(old, _value);
            }
        }
    }

    private void ValueChanged(T oldValue, T newValue)
    {
        if (OnValueChanged != null)
        {
            OnValueChanged(oldValue, newValue);
        }
    }

    public override string ToString()
    {
        return (Value != null ? Value.ToString() : "null");
    }
}